home *** CD-ROM | disk | FTP | other *** search
/ BCI NET 2 / BCI NET 2.iso / archives / programming / misc / xref_v1.1.lha / XRef / Tools / lib / scanwindow.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-09-24  |  8.7 KB  |  334 lines

  1. /*
  2. ** $PROJECT: xrefsupport.lib
  3. **
  4. ** $VER: scanwindow.c 1.3 (22.09.94) 
  5. **
  6. ** by
  7. **
  8. ** Stefan Ruppert , Windthorststraße 5 , 65439 Flörsheim , GERMANY
  9. **
  10. ** (C) Copyright 1994
  11. ** All Rights Reserved !
  12. **
  13. ** $HISTORY:
  14. **
  15. ** 22.09.94 : 001.003 :  now shows the time correctly (including hours)
  16. ** 17.09.94 : 001.002 :  status line added
  17. ** 09.09.94 : 001.001 :  initial
  18. */
  19.  
  20. /* ------------------------------- includes ------------------------------- */
  21.  
  22. #include "/source/def.h"
  23.  
  24. #include "xrefsupport.h"
  25.  
  26. /* ------------------------------- defines -------------------------------- */
  27.  
  28. #define WIN_FLAGS    (WFLG_CLOSEGADGET | WFLG_DRAGBAR | WFLG_DEPTHGADGET | \
  29.                       WFLG_ACTIVATE)
  30. #define WIN_IDCMP    (IDCMP_CLOSEWINDOW | IDCMP_VANILLAKEY)
  31.  
  32. #define XDISTANCE    10
  33. #define YDISTANCE    3
  34.  
  35. /* -------------------------- static data items --------------------------- */
  36.  
  37. static STRPTR gaugetexts[] = {
  38.    "Actual ",
  39.    "Total ",
  40.    NULL};
  41.  
  42. static STRPTR timestrs[] = {
  43.    "Time used",
  44.    "Exp.:",
  45.    "Left:",
  46.    NULL};
  47.  
  48. static STRPTR statustxt = "Status";
  49.  
  50. /* --------------------------- local prototypes --------------------------- */
  51.  
  52. static void draw_infotexts(struct ScanWindow *swin,STRPTR *texts);
  53.  
  54. /* ------------------------------ functions ------------------------------- */
  55.  
  56. BOOL open_scanwindow(struct ScanWindow *swin,STRPTR *texts,STRPTR title,UWORD winwidth)
  57. {
  58.    struct Screen *scr;
  59.    STRPTR *array = texts;
  60.    ULONG num = 1;
  61.  
  62.    while(*array++)
  63.       num++;
  64.  
  65.    if((scr = LockPubScreen(NULL)))
  66.    {
  67.       struct DrawInfo *dri;
  68.  
  69.       UWORD width  = scr->Width;
  70.       UWORD height = scr->Height;
  71.  
  72.       UWORD winheight; 
  73.  
  74.       UBYTE shinepen     = 2;
  75.       UBYTE shadowpen    = 1;
  76.       UBYTE highlightpen = 2;
  77.  
  78.       swin->sw_TextAttr = *scr->Font;
  79.       strcpy(swin->sw_FontName,swin->sw_TextAttr.ta_Name);
  80.       swin->sw_TextAttr.ta_Name = swin->sw_FontName;
  81.  
  82.       winheight = (swin->sw_TextAttr.ta_YSize + 1) * (num + 7);
  83.  
  84.       swin->sw_TextPen  = 1;
  85.       swin->sw_BackPen  = 0;
  86.  
  87.       if((dri = GetScreenDrawInfo(scr)))
  88.       {
  89.          swin->sw_TextPen = dri->dri_Pens[TEXTPEN];
  90.          swin->sw_BackPen = dri->dri_Pens[BACKGROUNDPEN];
  91.  
  92.          swin->sw_Total.FillPen  =
  93.          swin->sw_Actual.FillPen = dri->dri_Pens[FILLPEN];
  94.  
  95.          shinepen     = dri->dri_Pens[SHINEPEN];
  96.          shadowpen    = dri->dri_Pens[SHADOWPEN];
  97.          highlightpen = dri->dri_Pens[HIGHLIGHTTEXTPEN];
  98.  
  99.          FreeScreenDrawInfo(scr,dri);
  100.       }
  101.  
  102.       if((swin->sw_TextFont = OpenFont(&swin->sw_TextAttr)))
  103.       if((swin->sw_Window = OpenWindowTags(NULL,
  104.                                          WA_Title     ,title,
  105.                                          WA_Width     ,winwidth,
  106.                                          WA_Height    ,winheight,
  107.                                          WA_Left      ,(width  - winwidth ) >> 1,
  108.                                          WA_Top       ,(height - winheight) >> 1,
  109.                                          WA_Flags     ,WIN_FLAGS,
  110.                                          WA_IDCMP     ,WIN_IDCMP,
  111.                                          TAG_DONE)))
  112.       {
  113.          struct Window   *win = swin->sw_Window;
  114.          struct RastPort *rp  = win->RPort;
  115.          ULONG i;
  116.          UWORD x;
  117.  
  118.          SetFont(rp,swin->sw_TextFont);
  119.  
  120.          swin->sw_XMax = win->Width - win->BorderRight - XDISTANCE - 1;
  121.  
  122.          SetAPen(rp,highlightpen);
  123.  
  124.          draw_infotexts(swin,texts);
  125.  
  126.          swin->sw_YTime       = swin->sw_YTop + num * swin->sw_YStep + swin->sw_YStep / 2;
  127.  
  128.          Move(rp,swin->sw_XTop,swin->sw_YTime);
  129.          Text(rp,":",1);
  130.  
  131.          swin->sw_XTop += TextLength(rp,": ",2);
  132.  
  133.          swin->sw_XTime       = calctextwidth(rp,timestrs);
  134.          if(swin->sw_XTop > swin->sw_XTime)
  135.             swin->sw_XTime = swin->sw_XTop;
  136.  
  137.          /* maximal time length in pixel */
  138.          swin->sw_XTimeMax    = 5 * getmaxdigitwidth(rp) + 2 * TextLength(rp,":",1);
  139.  
  140.          x = swin->sw_XMax / 3;
  141.          for(i = 0 ; i < 3 ; i++)
  142.          {
  143.             Move(rp,i * x + XDISTANCE + ((i > 0) ? (swin->sw_XTimeMax/3) : 0),swin->sw_YTime);
  144.             Text(rp,timestrs[i],strlen(timestrs[i]));
  145.          }
  146.  
  147.          swin->sw_Total.Left  =
  148.          swin->sw_Actual.Left = calctextwidth(rp,gaugetexts) + XDISTANCE + win->BorderLeft;
  149.  
  150.          swin->sw_Total.Right  =
  151.          swin->sw_Actual.Right = swin->sw_XMax;
  152.  
  153.          swin->sw_Actual.Top    = swin->sw_YTop       + (num + 1) * swin->sw_YStep + swin->sw_YStep / 2;
  154.          swin->sw_Actual.Bottom = swin->sw_Actual.Top + swin->sw_YStep;
  155.  
  156.          swin->sw_Total.Top     = swin->sw_Actual.Bottom + swin->sw_YStep / 2;
  157.          swin->sw_Total.Bottom  = swin->sw_Total.Top     + swin->sw_YStep;
  158.  
  159.          Move(rp,XDISTANCE,swin->sw_Actual.Top + rp->TxBaseline + 1);
  160.          Text(rp,gaugetexts[0],strlen(gaugetexts[0]));
  161.  
  162.          Move(rp,XDISTANCE,swin->sw_Total.Top  + rp->TxBaseline + 1);
  163.          Text(rp,gaugetexts[1],strlen(gaugetexts[1]));
  164.  
  165.          draw_gaugeinit(win,&swin->sw_Actual,shinepen,shadowpen);
  166.          draw_gaugeinit(win,&swin->sw_Total ,shinepen,shadowpen);
  167.       }
  168.  
  169.       UnlockPubScreen(NULL,scr);
  170.    }
  171.  
  172.    return((BOOL) swin->sw_Window);
  173. }
  174.  
  175. void close_scanwindow(struct ScanWindow *swin,BOOL abort)
  176. {
  177.    if(swin->sw_Window)
  178.    {
  179.       if(!abort)
  180.       {
  181.          ULONG mask = (1<<swin->sw_Window->UserPort->mp_SigBit) | SIGBREAKF_CTRL_C;
  182.          ULONG rcvd;
  183.          struct IntuiMessage *msg;
  184.          BOOL end = FALSE;
  185.  
  186.          draw_scanwindowstatus(swin,"finished !");
  187.  
  188.          while(!end)
  189.          {
  190.             rcvd = Wait(mask);
  191.  
  192.             if(rcvd & SIGBREAKF_CTRL_C)
  193.                end = TRUE;
  194.             else
  195.                while((msg = (struct IntuiMessage *) GetMsg(swin->sw_Window->UserPort)))
  196.                {
  197.                   switch(msg->Class)
  198.                   {
  199.                   case IDCMP_VANILLAKEY:
  200.                      end = (msg->Code == 3) || (msg->Code == 27);
  201.                      break;
  202.                   case IDCMP_CLOSEWINDOW:
  203.                      end = TRUE;
  204.                      break;
  205.                   }
  206.                   ReplyMsg((struct Message *) msg);
  207.                }
  208.          }
  209.       }
  210.  
  211.       if(swin->sw_TextFont)
  212.          CloseFont(swin->sw_TextFont);
  213.  
  214.       CloseWindow(swin->sw_Window);
  215.    }
  216. }
  217.  
  218. static void draw_infotexts(struct ScanWindow *swin,STRPTR *texts)
  219. {
  220.    struct Window   *win = swin->sw_Window;
  221.    struct RastPort *rp  = win->RPort;
  222.  
  223.    STRPTR *array = texts;
  224.  
  225.    UWORD top = win->BorderTop + rp->TxBaseline + 2;
  226.    UWORD max = TextLength(rp,statustxt,strlen(statustxt));
  227.    UWORD x;
  228.  
  229.    SetBPen(rp,swin->sw_BackPen);
  230.    SetDrMd(rp,JAM2);
  231.  
  232.    swin->sw_YStep = rp->TxHeight + 1;
  233.    swin->sw_YTop  = top;
  234.  
  235.    Move(rp,XDISTANCE,top);
  236.    Text(rp,statustxt,strlen(statustxt));
  237.  
  238.    top += swin->sw_YStep;
  239.  
  240.    while(*array)
  241.    {
  242.       if((x = TextLength(rp,*array,strlen(*array))) > max)
  243.          max = x;
  244.  
  245.       Move(rp,XDISTANCE,top);
  246.       Text(rp,*array,strlen(*array));
  247.  
  248.       top += swin->sw_YStep;
  249.  
  250.       array++;
  251.    }
  252.  
  253.    max += 10 + XDISTANCE;
  254.  
  255.    array = texts;
  256.    top = swin->sw_YTop;
  257.  
  258.    Move(rp,max,top);
  259.    Text(rp,":",1);
  260.  
  261.    while(*array)
  262.    {
  263.       top += swin->sw_YStep;
  264.       Move(rp,max,top);
  265.       Text(rp,":",1);
  266.       array++;
  267.    }
  268.    
  269.    swin->sw_XTop = max;
  270. }
  271.  
  272. void draw_scanwindowstatus(struct ScanWindow *swin,STRPTR string)
  273. {
  274.    draw_scanwindowtext(swin,-1,string);
  275. }
  276.  
  277. void draw_scanwindowtext(struct ScanWindow *swin,ULONG num,STRPTR string)
  278. {
  279.    struct TextExtent txtext;
  280.    struct Window   *win = swin->sw_Window;
  281.    struct RastPort *rp  = win->RPort;
  282.  
  283.    UWORD y     = swin->sw_YTop + (++num) * swin->sw_YStep;
  284.    UWORD width = swin->sw_XMax - swin->sw_XTop;
  285.    ULONG len   = strlen(string);
  286.    UWORD chars;
  287.  
  288.    SetAPen(rp,swin->sw_TextPen);
  289.    SetBPen(rp,swin->sw_BackPen);
  290.    SetDrMd(rp,JAM2);
  291.  
  292.    Move(rp,swin->sw_XTop,y);
  293.  
  294.    if((chars = TextFit(rp,&string[len],len,&txtext,NULL,-1,width,swin->sw_YStep)) > 0)
  295.       Text(rp,&string[len - chars],chars);
  296.  
  297.    if(rp->cp_x < swin->sw_XMax)
  298.    {
  299.       y -= rp->TxBaseline;
  300.       EraseRect(rp,rp->cp_x,y, swin->sw_XMax , y + swin->sw_YStep - 1);
  301.    }
  302. }
  303.  
  304. void draw_scanwindowtime(struct ScanWindow *swin,ULONG *secs)
  305. {
  306.    struct RastPort *rp = swin->sw_Window->RPort;
  307.    UBYTE buf[100];
  308.    ULONG i;
  309.    UWORD x;
  310.    UWORD y;
  311.  
  312.    SetDrMd(rp,JAM2);
  313.    SetAPen(rp,swin->sw_TextPen);
  314.    SetBPen(rp,swin->sw_BackPen);
  315.  
  316.    for(i = 0 ; i < 3 ; i++)
  317.    {
  318.       sprintf(buf,"%01ld:%02ld:%02ld",((secs[i] / 3600) % 10),((secs[i] / 60) % 60),(secs[i] % 60));
  319.  
  320.       x = i * swin->sw_XMax / 3 + swin->sw_XTime - ((i) ? (swin->sw_XTimeMax/2) : 0);
  321.       y = swin->sw_YTime;
  322.  
  323.       Move(rp,x,y);
  324.       Text(rp,buf,strlen(buf));
  325.  
  326.       if(rp->cp_x < x + swin->sw_XTimeMax)
  327.       {
  328.          y -= rp->TxBaseline;
  329.          EraseRect(rp,rp->cp_x,y, x + swin->sw_XTimeMax , y + swin->sw_YStep - 1);
  330.       }
  331.    }
  332. }
  333.  
  334.